#include "peripheral.h"
#include "mc_interface.h"
#include "hardware_config.h"
#include "task_scheduler.h"

LEDPWM PWMDuty = {.Start=0,/*.ONOFF=0,*/.LedON=1,.FanON=0};

WIRELESS_REC WrilessRec;
WIRELESS_DAT WrilessDat = {.addr = 0, .cmd = 0, .crc = 0, .num = 0, .numLast = 0xff};

BEEP Beep = {.cnt = BEEP_ON_TIME, .status = 1};
ON_OFF_TIME OnOffTime = {.status = 0};

uint8_t crc=0;
uint8_t FAN_Shake = 0; 
//uint16_t FAN_Cmd = NO_COMMAND;
uint16_t pwmincr=0,pwmdecr=0;
uint16_t pwmk1k2=0;

uint32_t uwAddress = 0;
uint32_t readData = 0;
uint8_t Flagkey=0,ModeKey=0;
uint8_t FlagAC=0;
uint8_t FlagACOn=0;
uint8_t FlagSleep=0;
uint16_t SleepCnt=0;

uint8_t  SWOFirstflag  = 0;
uint8_t  SWOUSEflag  = 0;

uint8_t  McuSleepCnt =0;
 
uint8_t FanLightStatus = 0;
//uint8_t SEGKey=0;

uint8_t FanSartCnt = 0;
/*void BeepTest(void)
{
    RELAY_PORT->BIT_SET_RST = RELAY_PIN;
}
*/
uint16_t LEDCnt=0;

//-------------------------------------------------------------
void FlashWrite(uint32_t writeBuf)
{
		FLASH_Unlock();
		FLASH_ErasePage(FLASH_OPERATE_ADDR);
		FLASH_ProgramWord(FLASH_OPERATE_ADDR, writeBuf); //addr+4
		FLASH_Lock();
}

//-------------------------------------------------------------
void FlashRead(void)
{
	uint32_t data=0;
	
	FLASH_Unlock();
	data=FLASH_ReadWord(FLASH_OPERATE_ADDR);
	FLASH_Lock();
	
	if(data<5)
	{
		ModeKey=data;
	}
}

//-------------------------------------------------------------
void LED_PWM_Init(void)
{
	GPIO_InitTypeStruct CCT_GPIO_InitStruct;
	CCT_TimeBaseInitTypeStruct CCT_TimeBaseInitStruct;
	
	/* Enable clock */
  RCC_Unlock();
  RCC_AHBPeriphClockCmd(RCC_AHB_PERIPH_GPIO, ENABLE);
  RCC_APBPeriphClockCmd(RCC_APB_PERIPH_CCT0, ENABLE);
  RCC_Lock();
	
	GPIO_StructInit(&CCT_GPIO_InitStruct);
	CCT_GPIO_InitStruct.GPIO_Mode = GPIO_MODE_INPUT; //
	CCT_GPIO_InitStruct.GPIO_Speed = GPIO_SPEED_HIGH;
	CCT_GPIO_InitStruct.GPIO_OType = GPIO_OTYPE_OD;  	
	CCT_GPIO_InitStruct.GPIO_PuPd = GPIO_PUPD_DOWN; // 下拉
	CCT_GPIO_InitStruct.GPIO_Pin = GPIO_PIN_13 | GPIO_PIN_14;
	GPIO_Init(GPIOA, &CCT_GPIO_InitStruct);
	
	CCT_GPIO_InitStruct.GPIO_Pin = AC_PIN;   //掉电检测
	GPIO_Init(AC_PORT, &CCT_GPIO_InitStruct);
	
	/* Config CCT0 GPIO */
  CCT_GPIO_InitStruct.GPIO_Mode = GPIO_MODE_ALT_FUNC;
  CCT_GPIO_InitStruct.GPIO_Speed = GPIO_SPEED_HIGH;
  CCT_GPIO_InitStruct.GPIO_OType = GPIO_OTYPE_PP;
  CCT_GPIO_InitStruct.GPIO_PuPd = GPIO_PUPD_NOPULL;
  CCT_GPIO_InitStruct.GPIO_Pin = GPIO_PIN_1;
  GPIO_Init(GPIOD, &CCT_GPIO_InitStruct);
  GPIO_AltFuncConfig(GPIOD, GPIO_PIN_1, GPIO_ALT_FUNC_5);  //cct_ch1
	
	CCT_GPIO_InitStruct.GPIO_Pin = GPIO_PIN_4;
  GPIO_Init(GPIOB, &CCT_GPIO_InitStruct);
  GPIO_AltFuncConfig(GPIOB, GPIO_PIN_4, GPIO_ALT_FUNC_1);  //cct_ch2
	
  /* Config LED GPIO */
  CCT_DeInit(CCT0);
//	CCT_DeInit(CCT1);
  /* Config CCT0 */
  CCT_TimeBaseStructInit(&CCT_TimeBaseInitStruct);
  CCT_TimeBaseInitStruct.CCT_CounterCycle = CCT_COUNTER_CYCLE_PERIOD;
  CCT_TimeBaseInitStruct.CCT_ClockDivision = CCT_CLK_DIV16;
  CCT_TimeBaseInitStruct.CCT_ReloadValue = 1500 - 1;  //1ms
	CCT_TimeBaseInit(CCT0, &CCT_TimeBaseInitStruct);
  CCT_Cmd(CCT0, ENABLE);
	
	//CCT_TimeBaseInit(CCT1, &CCT_TimeBaseInitStruct);
 // CCT_Cmd(CCT1, ENABLE);
	
  /* Config CCT0 output compare */
  CCT_OCInitTypeStruct CCT_OCInitStruct;
  CCT_OCStructInit(&CCT_OCInitStruct);
  CCT_OCInitStruct.CCT_OCPulse = 0;
  CCT_OCInitStruct.CCT_OCPolarity = CCT_OC_POLARITY_HIGH;
  CCT_OC1Init(CCT0, &CCT_OCInitStruct);
  CCT_CH1Cmd(CCT0, ENABLE);
	
	CCT_OC2Init(CCT0, &CCT_OCInitStruct);
  CCT_CH2Cmd(CCT0, ENABLE);
	
	CCT_SetCompare1(CCT0,0);
	CCT_SetCompare2(CCT0,0);
 // CCT_OC0Init(CCT1, &CCT_OCInitStruct);
 // CCT_CH0Cmd(CCT1, ENABLE);
	
	//PWMDuty.ONOFF=0;
//	PWMDuty.FanON=0;
//	PWMDuty.LedON=0;
//	pwmincr=0,pwmdecr=0;
//	PWMDuty.Start=0;
//	PWMDuty.Wdata=0;
//	PWMDuty.Ydata=0;
//	PWMDuty.WdataK=0;
//	PWMDuty.YdataK=0;
//	PWMDuty.startY=0;
//	PWMDuty.startW=0;
//	PWMDuty.CCT_Dec = 0;
//	PWMDuty.CCT_Inc = 0;
//	PWMDuty.Brightless_Dec = 0;
//	PWMDuty.Brightless_Inc = 0;
	
	//FlashRead();
//	ModeKey=0;
//	FlagAC=1;
}

//-----------------------------------------------------
void Peripheral_Init(void)
{
    GPIO_InitTypeStruct struGPIO_Init;
    TIM_InitTypeStruct struTIM_Init;
    
    RCC_Unlock();
    RCC_AHBPeriphClockCmd(RCC_AHB_PERIPH_GPIO, ENABLE);
    RCC_APBPeriphClockCmd(RCC_APB_PERIPH_TIM1, ENABLE);
//	RCC_APBPeriphClockCmd(RCC_APB_PERIPH_ACMP, ENABLE);
	  RCC_APBPeriphClockCmd(RCC_APB_PERIPH_CCT0, ENABLE);
    RCC_Lock();
    
    TIM_DeInit(TIM1);
    TIM_StructInit(&struTIM_Init);
    
    struTIM_Init.TIM_CounterCycle = TIM_COUNTER_CYCLE_SINGLE;
    struTIM_Init.TIM_IntState = ENABLE;
    struTIM_Init.TIM_ReloadValue = 240000;  //定时 5ms
    TIM_Init(TIM1, &struTIM_Init);
    
    TIM_IntCmd(TIM1, ENABLE);
    
	  GPIO_StructInit(&struGPIO_Init);
    struGPIO_Init.GPIO_Mode = GPIO_MODE_INPUT; //
    struGPIO_Init.GPIO_Speed = GPIO_SPEED_HIGH;
    struGPIO_Init.GPIO_OType = GPIO_OTYPE_PP;   // 推挽
    struGPIO_Init.GPIO_PuPd = GPIO_PUPD_NOPULL; // 无上下拉
    
    struGPIO_Init.GPIO_Pin = AC_PIN;
    GPIO_Init(AC_PORT, &struGPIO_Init);
	
    //继电器
    //GPIO_StructInit(&struGPIO_Init);
    //struGPIO_Init.GPIO_Mode = GPIO_MODE_OUTPUT; //
    //struGPIO_Init.GPIO_Speed = GPIO_SPEED_HIGH;
    //struGPIO_Init.GPIO_OType = GPIO_OTYPE_PP;   // 推挽
    //struGPIO_Init.GPIO_PuPd = GPIO_PUPD_NOPULL; // 无上下拉
    
    //struGPIO_Init.GPIO_Pin = RELAY_PIN;
    //GPIO_Init(RELAY_PORT, &struGPIO_Init);
    
    //蜂鸣器
    //struGPIO_Init.GPIO_Pin = BEEP_PIN;
    //GPIO_Init(BEEP_PORT, &struGPIO_Init);
    
    //Beep.status = 1;
		//Beep.cnt = BEEP_ON_TIME;
    
    
    //遥控接受引脚
       //配置引脚为中断模式，边沿触发
    GPIO_StructInit(&struGPIO_Init);
    struGPIO_Init.GPIO_Mode = GPIO_MODE_INPUT; //
    struGPIO_Init.GPIO_Speed = GPIO_SPEED_HIGH;
    struGPIO_Init.GPIO_OType = GPIO_OTYPE_PP;   // 推挽
    struGPIO_Init.GPIO_PuPd = GPIO_PUPD_UP; // 无上下拉
    
    struGPIO_Init.GPIO_Pin = WIRELESS_PIN;
    GPIO_Init(WIRELESS_PORT, &struGPIO_Init);
//    
//    
    GPIO_IntConfig(WIRELESS_PORT, WIRELESS_PIN, GPIO_INT_RISING_FALLING);
    GPIO_IntEnable(WIRELESS_PORT, WIRELESS_PIN);
    
    NVIC_SetPriority(WIRELESS_IRQn, 2);
    NVIC_EnableIRQ(WIRELESS_IRQn);
    NVIC_SetPriority(TIM1_IRQn, 3);
    NVIC_EnableIRQ(TIM1_IRQn);	
		
		//----------------------------
	//	LED_PWM_Init();
			PWMDuty.FanON=0;
			PWMDuty.LedON=0;
			pwmincr=0,pwmdecr=0;
			PWMDuty.Start=0;
			PWMDuty.Wdata=0;
			PWMDuty.Ydata=0;
			PWMDuty.WdataK=0;
			PWMDuty.YdataK=0;
			PWMDuty.startY=0;
			PWMDuty.startW=0;
			PWMDuty.CCT_Dec = 0;
			PWMDuty.CCT_Inc = 0;
			PWMDuty.Brightless_Dec = 0;
			PWMDuty.Brightless_Inc = 0;
			PWMDuty.Brightless     = LED_BRIGHTLESS_MAX;
			
			ModeKey=0;
	    FlagAC=1;
			FanLightStatus= 0;
}

//定时中断
void TIM1_IRQHandler(void)
{
    if(TIM_GetIntFlagStatus(TIM1) == SET)
    {
        TIM_ClearIntFlag(TIM1);
        WrilessRec.RecBitCnt=0;
    }
}

//信号接收口中断
void WIRELESS_IRQHandler(void)
{
    uint32_t TimCnt = 0;
    
    if(GPIO_GetIntFlagStatus(WIRELESS_PORT, WIRELESS_PIN) == SET)
    {
        GPIO_ClearIntFlag(WIRELESS_PORT, WIRELESS_PIN);
        
        TimCnt = TIM1->ARR - TIM1->CNT;
        TIM1->CNT = TIM1->ARR;
				
        if(GPIO_ReadInputBit(WIRELESS_PORT, WIRELESS_PIN) == BIT_SET)
        {
            TIM_Cmd(TIM1, ENABLE);
        }
        else 
        {
            if(WrilessRec.RecComplete == 0)
            {
								// 数据为0,否则为1
								if(TimCnt < 54000 && TimCnt > 4800) //IR,0==560,1==1680
								{
										WrilessRec.RecBitBuf[WrilessRec.RecBitCnt++] = 0;
								}
								else if(TimCnt > 54000 && TimCnt < 96000)
								{
										WrilessRec.RecBitBuf[WrilessRec.RecBitCnt++] = 1;
								}
								else if(TimCnt > 96000 && TimCnt < 120000) //2ms-2.5ms
								{
										if(WrilessRec.FirstLong==1)
										{
												WrilessRec.RecNum++;
												if(WrilessRec.RecNum>=4)
												{
													 WrilessRec.RecNum=4;
													 WrilessRec.KeyLong=1;
													 WrilessRec.RecComplete=1;
													 LEDCnt=0;
												}
										}
								}
								//------------------------------------------------------
								if(WrilessRec.RecBitCnt >= 32) //33bit
                {
										WrilessRec.RecBitCnt=0;
                    WrilessRec.RecComplete=1;
										WrilessRec.FirstLong=0;
										if(WrilessRec.KeyLong!=1)WrilessRec.KeyLong=0;
										WrilessRec.RecNum=0;
                }
            }
        }
    }
}

//*********************************************************
void WrilessRec_Handler(void)
{
	//static uint8_t SEGkey = 0;
	  uint8_t i;
	
	  SWO_reuseGPIO();

		if(FlagACOn==0)
		{
			SleepMcu(); //20250429
			LED_ACONOFF();
			//------------------------------------------
			if(WrilessRec.RecComplete == 1)
			{
					WrilessRec.RecComplete = 0;
					if(WrilessRec.FirstLong==0)
					{
						WrilessDat.addr = 0;
						for(i = 0;i < 16;i++)
						{
								WrilessDat.addr <<= 1;
								WrilessDat.addr |= WrilessRec.RecBitBuf[i];
						}
						
						//--------------------------------------------------------
						WrilessDat.crc = 0;
						for(i = 0;i < 8;i++)
						{
								WrilessDat.crc <<= 1;
								WrilessDat.crc |= WrilessRec.RecBitBuf[16+i];
						}
						crc= ~WrilessDat.crc;
						
						//---------------------------------------------------------
						WrilessDat.cmd = 0;
						for(i = 0;i < 8;i++)
						{
								WrilessDat.cmd <<= 1;
								WrilessDat.cmd |= WrilessRec.RecBitBuf[24+i];
						}
					}
					
					//---------------------------------------------------------
					if((WrilessDat.addr==0x00FF) && (WrilessDat.cmd == crc))
					{
							switch(WrilessDat.cmd)
							{		
									case 0x5D:
										if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
										{
											FanLightStatus = 0;
											if(PWMDuty.FanON == 1)
											{FanLightStatus |= 0x01;}
											if(PWMDuty.LedON == 1)
											{FanLightStatus |= 0x02;}
											PWMDuty.FanON = 0;
											OnOffTime.status = 0;
											FAN_Shake = 0;
											Command = CMD_STOP;
											PWMDuty.LedON =0;
										}
										else 
										{
											if((FanLightStatus&0x01) == 1)
											{
												PWMDuty.FanON = 1;
												Command = CMD_START;
											}
											if(FanLightStatus>>1 == 1)
											{
												PWMDuty.LedON =1;
											}
										}
										break;
									//-------------------------------------FAN
									case 0xE7:  //FAN-ON/OFF
											if(PWMDuty.FanON == 1)
											{
												FanLightStatus &= 0x02;
												PWMDuty.FanON=0;
												OnOffTime.status = 0;
												FAN_Shake = 0;
												Command = CMD_STOP;
											}
											else 
											{
												PWMDuty.FanON=1;
												Command = CMD_START;
											}
											break;
											
									case 0xEF:   //1档
                      #if(CONTROL_MODE == 0)
                        SignalChain.hPowref = LEVEL1_SPEED_CMD;                   
											#else
                        SignalChain.hPowref = LEVEL1_POWER_CMD;
											#endif
											Command = CMD_START;
											PWMDuty.FanON=1;
											break;
																		
									case 0xC7:   //3档
                      #if(CONTROL_MODE == 0)
                         SignalChain.hPowref = LEVEL3_SPEED_CMD;                   
											#else
                        SignalChain.hPowref = LEVEL2_POWER_CMD;
											#endif
											Command = CMD_START;
											PWMDuty.FanON=1;
											break;
									
									case 0xA5:   //6档
                      #if(CONTROL_MODE == 0)
                         SignalChain.hPowref = LEVEL6_SPEED_CMD;                   
											#else
                        SignalChain.hPowref = LEVEL3_POWER_CMD;
											#endif
											Command = CMD_START;
											PWMDuty.FanON=1;
											break;
									
									case 0xBD: //定时1小时
											if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
											{
													OnOffTime.timeCnt = 0;
													OnOffTime.timeEnd = 7200;
													OnOffTime.status = 1;
											}
											
											PWMDuty.Cnt=40;
											break;
											
									case 0xB5:   //定时2小时
											if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
											{
												OnOffTime.timeCnt = 0;
												OnOffTime.timeEnd = 14400;
												OnOffTime.status = 1;
											}
											PWMDuty.Cnt=40;
											break;
											
									case 0xAD:   //定时4小时
											if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
											{
												OnOffTime.timeCnt = 0;
												OnOffTime.timeEnd = 28800;
												OnOffTime.status = 1;
											}
											PWMDuty.Cnt=40;
											break;
											
									//+++++++++++++++++++++++++++++++++++++LED
									case 0x9D:   //LED开关
											if(PWMDuty.LedON==1)
												{
													PWMDuty.LedON =0;
													FanLightStatus &= 0x01;
												}
											else PWMDuty.LedON =1;
											break;
											
									case 0x6F:  //亮度++
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.Brightless_Inc=1;
												}
												else if(WrilessRec.KeyLong==1)
												{
													PWMDuty.Brightless_Inc=2;
													WrilessRec.KeyLong=2;
													
												}
												WrilessRec.FirstLong=1;
											}
											
											break;
									
									case 0x1F:  //亮度--
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.Brightless_Dec=1;
												}
												else if(WrilessRec.KeyLong==1)
												{
													PWMDuty.Brightless_Dec=2;
													WrilessRec.KeyLong=2;
												}
												WrilessRec.FirstLong=1;
											}
											
											break;		
									
									case   0x4F://色温++
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.CCT_Inc=1;
												}
												else if(WrilessRec.KeyLong==1)
												{
													PWMDuty.CCT_Inc=2;
													WrilessRec.KeyLong=2;
												}
												WrilessRec.FirstLong=1;
											}
											
											break;	
									
									case    0x97:  //色温--
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.CCT_Dec=1;
												}
												else if(WrilessRec.KeyLong==1) //长按
												{
													PWMDuty.CCT_Dec=2;
													WrilessRec.KeyLong=2;
												}
												WrilessRec.FirstLong=1;
											}
											
											break;
											
									case 0xDD:
											FlagAC=0;
											PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											PWMDuty.startW=0;
											PWMDuty.LedON=1;
											ModeKey=0;
											//data=ModeKey;
											//FlashWrite(data);
											break;
									
									case 0xFD:
											FlagAC=0;
											PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											PWMDuty.startW=128;
											PWMDuty.LedON=1;
											ModeKey=4;
											//data=ModeKey;
											//FlashWrite(data);
											break;
									
									case 0x3D:
											FlagAC=0;
											PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											PWMDuty.startW=256;
											PWMDuty.LedON=1;
											ModeKey=3;
											//data=ModeKey;
											//FlashWrite(data);
											break;
									
									case 0x57:	
									    if(ModeKey==0 || ModeKey==1)
											{
											  PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											  PWMDuty.startW=0;
											  PWMDuty.LedON=1;
												ModeKey=3;
											}else if(ModeKey==2 || ModeKey==5)
											{
											  PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											  PWMDuty.startW=0x100;
											  PWMDuty.LedON=1;
												ModeKey=0;
											}else if(ModeKey == 3)
											{
											  PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											  PWMDuty.startW=128;
											  PWMDuty.LedON=1;
												ModeKey=4;
											}else if(ModeKey == 4)
											{
											  PWMDuty.Brightless=150;
											  PWMDuty.startW=128;
											  PWMDuty.LedON=1;
												ModeKey=5;
											}
											//data=ModeKey;
											//FlashWrite(data);
											break;
									
									case 0x1D: //小夜灯
											FlagAC=0;
											PWMDuty.LedON=1;
									    PWMDuty.Brightless=150;
											PWMDuty.startW=128;
											ModeKey=5;
												//data=ModeKey;
												//FlashWrite(data);
											
											break;
									
									default:
											break;
						 }
						FAN_Cmd= 	Command ;
				  }
					
				//---------------------------------------------------------
					if((WrilessDat.addr==0x807F) && (WrilessDat.cmd == crc))
					{
							switch(WrilessDat.cmd)
							{		
									case 0xFF:
										if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
										{
											FanLightStatus = 0;
											if(PWMDuty.FanON == 1)
											{FanLightStatus |= 0x01;}
											if(PWMDuty.LedON == 1)
											{FanLightStatus |= 0x02;}
											PWMDuty.FanON = 0;
											OnOffTime.status = 0;
											FAN_Shake = 0;
											Command = CMD_STOP;
											PWMDuty.LedON =0;
										}
										else 
										{
											if((FanLightStatus&0x01) == 1)
											{
												PWMDuty.FanON = 1;
												Command = CMD_START;
											}
											if(FanLightStatus>>1 == 1)
											{
												PWMDuty.LedON =1;
											}
										}
										break;
									//-------------------------------------FAN
									case 0x6F:  //FAN-ON/OFF
											if(PWMDuty.FanON == 1)
											{
												FanLightStatus &= 0x02;
												PWMDuty.FanON=0;
												OnOffTime.status = 0;
												FAN_Shake = 0;
												Command = CMD_STOP;
											}
											else 
											{
												PWMDuty.FanON=1;
												Command = CMD_START;
											}
											break;
											
									case 0x3F:   //1档
                      #if(CONTROL_MODE == 0)
                        SignalChain.hPowref = LEVEL1_SPEED_CMD;                   
											#else
                        SignalChain.hPowref = LEVEL1_POWER_CMD;
											#endif
											Command = CMD_START;
											PWMDuty.FanON=1;
											break;
																		
									case 0x1F:   //3档
                      #if(CONTROL_MODE == 0)
                         SignalChain.hPowref = LEVEL3_SPEED_CMD;                   
											#else
                        SignalChain.hPowref = LEVEL2_POWER_CMD;
											#endif
											Command = CMD_START;
											PWMDuty.FanON=1;
											break;
									
									case 0x2F:   //6档
                      #if(CONTROL_MODE == 0)
                         SignalChain.hPowref = LEVEL6_SPEED_CMD;                   
											#else
                        SignalChain.hPowref = LEVEL3_POWER_CMD;
											#endif
											Command = CMD_START;
											PWMDuty.FanON=1;
											break;
									
//									case 0xBD: //定时1小时
//											if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
//											{
//													OnOffTime.timeCnt = 0;
//													OnOffTime.timeEnd = 7200;
//													OnOffTime.status = 1;
//											}
//											
//											PWMDuty.Cnt=10;
//											break;
											
									case 0xFD:   //定时2小时
											if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
											{
												OnOffTime.timeCnt = 0;
												OnOffTime.timeEnd = 14400;
												OnOffTime.status = 1;
											}
											PWMDuty.Cnt=40;
											break;
											
									case 0xED:   //定时4小时
											if(PWMDuty.FanON==1 || PWMDuty.LedON==1)
											{
												OnOffTime.timeCnt = 0;
												OnOffTime.timeEnd = 28800;
												OnOffTime.status = 1;
											}
											PWMDuty.Cnt=40;//20->
											break;
											
									//+++++++++++++++++++++++++++++++++++++LED
									case 0x7F:   //LED开关
											if(PWMDuty.LedON==1)
											{
												FanLightStatus &= 0x01;

												PWMDuty.LedON =0;
											}
											else PWMDuty.LedON =1;
											break;
											
									case 0x0AF:  //亮度++
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.Brightless_Inc=1;
													
												}
												else if(WrilessRec.KeyLong==1)
												{
													PWMDuty.Brightless_Inc=2;
													WrilessRec.KeyLong=2;
												}
												WrilessRec.FirstLong=1;
											}
											
											break;
									
									case 0xBF:  //亮度--
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.Brightless_Dec=1;
												}
												else if(WrilessRec.KeyLong==1)
												{
													PWMDuty.Brightless_Dec=2;
													WrilessRec.KeyLong=2;
												}
												WrilessRec.FirstLong=1;
											}
											
											break;		
									
									case   0x05F://色温++
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.CCT_Inc=1;
												}
												else if(WrilessRec.KeyLong==1)
												{
													PWMDuty.CCT_Inc=2;
													WrilessRec.KeyLong=2;
												}
												WrilessRec.FirstLong=1;
											}
											
											break;	
									
									case    0xDF:  //色温--
											if(PWMDuty.LedON==1)
											{
												if(WrilessRec.KeyLong==0)
												{
													PWMDuty.CCT_Dec=1;
												}
												else if(WrilessRec.KeyLong==1)
												{
													PWMDuty.CCT_Dec=2;
													WrilessRec.KeyLong=2;
												}
												WrilessRec.FirstLong=1;
											}
											
											break;
											
//									case 0xDD:
//											FlagAC=0;
//											PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
//											PWMDuty.startW=0;
//											PWMDuty.LedON=1;
//											//ModeKey=0;
//											//data=ModeKey;
//											//FlashWrite(data);
//											break;
//									
//									case 0xFD:
//											FlagAC=0;
//											PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
//											PWMDuty.startW=128;
//											PWMDuty.LedON=1;
//											//ModeKey=4;
//											//data=ModeKey;
//											//FlashWrite(data);
//											break;
//									
//									case 0x3D:
//											FlagAC=0;
//											PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
//											PWMDuty.startW=256;
//											PWMDuty.LedON=1;
//											//ModeKey=3;
//											//data=ModeKey;
//											//FlashWrite(data);
//											break;
									
									case 0x9F:	
											//ModeKey++; //切色温
											//if(ModeKey>=5)ModeKey=0;
											//FlagAC=1;
											//data=ModeKey;
											//FlashWrite(data);
									    if(ModeKey==0 || ModeKey==1)
											{
											  PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											  PWMDuty.startW=0;
											  PWMDuty.LedON=1;
												ModeKey=3;
											}else if(ModeKey==2 || ModeKey==5)
											{
											  PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											  PWMDuty.startW=0x100;
											  PWMDuty.LedON=1;
												ModeKey=0;
											}else if(ModeKey == 3)
											{
											  PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
											  PWMDuty.startW=128;
											  PWMDuty.LedON=1;
												ModeKey=4;
											}else if(ModeKey == 4)
											{
											  PWMDuty.Brightless=150;
											  PWMDuty.startW=128;
											  PWMDuty.LedON=1;
												ModeKey=5;
											}
											break;
									
									case 0xDD: //小夜灯
											FlagAC=0;
											PWMDuty.LedON=1;
									    PWMDuty.Brightless=150;
											PWMDuty.startW=128;
											ModeKey=5;
												//data=ModeKey;
												//FlashWrite(data);
											
											break;
									
									default:
											break;
						 }
						FAN_Cmd= 	Command ;
				  }
			}
	 }
}
//----------------------------------------------
void LED_OFF(void)
{
	FlagAC=0;
	PWMDuty.LedON = 0;
	PWMDuty.Start=6;
	PWMDuty.startY=PWMDuty.YdataK;
	PWMDuty.startW=PWMDuty.WdataK;
	PWMDuty.YdataK=0;
	PWMDuty.WdataK=0;
	pwmincr=0;pwmdecr=0;
}
//----------------------------------------------
void LEDIncrDecr(void)
{
	if(PWMDuty.LedON==0)
	{
		PWMDuty.LedON=1;
		PWMDuty.Start=2;
		PWMDuty.startY=0;
		PWMDuty.startW=0;
		PWMDuty.Cnt=0;
	}
	else 
	{
		PWMDuty.Start=7;
		PWMDuty.Cnt=0;
	}
}

//*********************************************************
void LED_ACONOFF(void)
{
		if(FlagAC==1)
		{
			FlagAC=0;
			if(ModeKey==0)
			{
				PWMDuty.LedON     = 1;
				PWMDuty.Brightless=LED_BRIGHTLESS_MAX;
				PWMDuty.startW=256;
				
				if(PWMDuty.FanON==1)
				{
					PWMDuty.FanON=0;
					OnOffTime.status = 0;
					FAN_Shake = 0;
					Command = CMD_STOP;
					FAN_Cmd = Command;
				}
			}
			else if(ModeKey==1)
			{
				PWMDuty.LedON      = 1;
				PWMDuty.Brightless =LED_BRIGHTLESS_MAX;
				PWMDuty.startW      =256;
				
				FanSartCnt = 60;
//				PWMDuty.FanON=1;
//				Command = CMD_START;
//				FAN_Cmd = Command;
//				SignalChain.hPowref = LEVEL2_POWER_CMD;
			}
			else if(ModeKey==2)
			{
				PWMDuty.LedON=0;
				
				FanSartCnt = 60;
//				PWMDuty.FanON=1;
//				Command = CMD_START;
//				FAN_Cmd = Command;
//				SignalChain.hPowref = LEVEL2_POWER_CMD;
			}
			else if(ModeKey==3)
			{
				PWMDuty.LedON      = 1;
				PWMDuty.Brightless =LED_BRIGHTLESS_MAX;
				PWMDuty.startW      =0;
				
				if(PWMDuty.FanON==1)
				{
					PWMDuty.FanON=0;
					OnOffTime.status = 0;
					FAN_Shake = 0;
					Command = CMD_STOP;
					FAN_Cmd = Command;
				}
			}
			else if(ModeKey==4)
			{
				PWMDuty.LedON      = 1;
				PWMDuty.Brightless =LED_BRIGHTLESS_MAX;
				PWMDuty.startW      =128;
				
				if(PWMDuty.FanON==1)
				{
					PWMDuty.FanON=0;
					OnOffTime.status = 0;
					FAN_Shake = 0;
					Command = CMD_STOP;
				}
			}
	 }
}

//*****************************************************

void LED_PWMdispose(void)
{
	static uint16_t CCTCnt=0;
	
	//亮度
	if(PWMDuty.Brightless_Inc==1)  //++
	{
		PWMDuty.Brightless_Inc =0;	
		
		PWMDuty.Brightless +=LED_BRIGHTLESS_INC;
		if(PWMDuty.Brightless >LED_BRIGHTLESS_MAX)
		{
			PWMDuty.Brightless = LED_BRIGHTLESS_MAX;
		}
	}
	else if(PWMDuty.Brightless_Inc==2) 
	{
		//PWMDuty.Brightless_Inc =0;	
		
		PWMDuty.Brightless +=3;
		if(PWMDuty.Brightless >LED_BRIGHTLESS_MAX)
		{
			PWMDuty.Brightless = LED_BRIGHTLESS_MAX;
		}
	}
	
	if(PWMDuty.Brightless_Dec==1)  //--
	{
		PWMDuty.Brightless_Dec =0;
		
		PWMDuty.Brightless -=LED_BRIGHTLESS_DEC;
		if(PWMDuty.Brightless <150)
		{
			PWMDuty.Brightless  =150;
		}
	}
	else if(PWMDuty.Brightless_Dec==2)
	{
		//PWMDuty.Brightless_Dec =0;
		
		PWMDuty.Brightless -=3;
		if(PWMDuty.Brightless <150)
		{
			PWMDuty.Brightless  =150;
		}
	}
	
	//色温
	if(PWMDuty.CCT_Inc==1)  //++
	{
		PWMDuty.CCT_Inc =0;
		
		PWMDuty.startW  +=LED_CCT_INC;
    if(PWMDuty.startW >0x100)
		{
			PWMDuty.startW = 0x100;
		}
    if(PWMDuty.Brightless<=150)
		{
			PWMDuty.startW = 0x100;
		}
    //PWMDuty.startY = 	0xFF-	PWMDuty.startW;
	}
	else if(PWMDuty.CCT_Inc==2) 
	{
		CCTCnt++;
		if(CCTCnt<2)PWMDuty.startW+=1;
		else if(CCTCnt>=3){CCTCnt=0;PWMDuty.startW+=1;}
		
    if(PWMDuty.startW >0x100)
		{
			PWMDuty.startW = 0x100;
		}
    if(PWMDuty.Brightless<=150)
		{
			PWMDuty.startW = 0x100;
		}
	}
	
	if(PWMDuty.CCT_Dec==1) //--
	{
		PWMDuty.CCT_Dec =0;
		PWMDuty.startW  -=LED_CCT_DEC;	
		if(PWMDuty.startW <0x0)
		{
			PWMDuty.startW = 0x0;
		}
		if(PWMDuty.Brightless<=150)
		{
			PWMDuty.startW = 0x0;
		}
		//PWMDuty.startY = 	0xFF-	PWMDuty.startW;
	}
	else if(PWMDuty.CCT_Dec==2)
	{
		CCTCnt++;
		if(CCTCnt<2)PWMDuty.startW-=1;
		else if(CCTCnt>=3){CCTCnt=0;PWMDuty.startW-=1;}
		if(PWMDuty.startW <0x0)
		{
			PWMDuty.startW = 0x0;
		}
		if(PWMDuty.Brightless<=150)
		{
			PWMDuty.startW = 0x0;
		}
	}
	
	if(PWMDuty.LedON)
	{
		PWMDuty.startY = 	0x100-	PWMDuty.startW;
		PWMDuty.YdataK = (PWMDuty.Brightless*PWMDuty.startY)>>8;
		if(PWMDuty.YdataK>1500)
		{
			PWMDuty.YdataK = 1500;
		}else if(PWMDuty.YdataK<150&&PWMDuty.YdataK>0)
		{
			PWMDuty.YdataK = 150;
		}
		
		PWMDuty.WdataK = (PWMDuty.Brightless*PWMDuty.startW)>>8;
		if(PWMDuty.WdataK>1500)
		{
			PWMDuty.WdataK = 1500;
		}else if(PWMDuty.WdataK<150&&PWMDuty.WdataK>0)
		{
			PWMDuty.WdataK = 150;
		}
		
		if(PWMDuty.Cnt>0)
		{
			PWMDuty.Cnt--;
			PWMDuty.WdataK = 0;
			PWMDuty.YdataK = 0;
		}
	}
	else
	{
		PWMDuty.YdataK = 0;
		PWMDuty.WdataK = 0;
		
		if(PWMDuty.Cnt>0)
		{
			PWMDuty.Cnt--;
			PWMDuty.WdataK = 750;
			PWMDuty.YdataK = 750;
		}
	}
	
}

void PWM_INCR(void)
{
	
	LED_PWMdispose();   //灯光指令处理
	
	if(PWMDuty.Ydata<PWMDuty.YdataK)
	{
		PWMDuty.Ydata+=50;
		if(PWMDuty.Ydata>PWMDuty.YdataK)
		{
			PWMDuty.Ydata =PWMDuty.YdataK;
		}
	}
	else if(PWMDuty.Ydata>PWMDuty.YdataK)
	{
		PWMDuty.Ydata-=50;
		if(PWMDuty.Ydata<PWMDuty.YdataK)
		{
			PWMDuty.Ydata =PWMDuty.YdataK;
		}
	}
	
	if(PWMDuty.Wdata<PWMDuty.WdataK)
	{
		PWMDuty.Wdata+=50;
		if(PWMDuty.Wdata>PWMDuty.WdataK)
		{
			PWMDuty.Wdata =PWMDuty.WdataK;
		}
	}
	else if(PWMDuty.Wdata>PWMDuty.WdataK)
	{
		PWMDuty.Wdata-=50;
		if(PWMDuty.Wdata<PWMDuty.WdataK)
		{
			PWMDuty.Wdata =PWMDuty.WdataK;
		}
	}
	CCT_SetCompare2(CCT0,PWMDuty.YdataK);
	CCT_SetCompare1(CCT0,PWMDuty.WdataK);
	
}

//-------------------------------------------------------------
void KeyScan(void)
{
	static uint16_t Cnt1=0,Cnt2=0,Cnt3=0;
	
	if(GPIO_ReadInputBit(AC_PORT, AC_PIN)==BIT_SET && Flagkey==1)
	{
		Cnt2=0;
		if(++Cnt1>20) //ms
		{
			Cnt1=0;Flagkey=0;
			
			ModeKey++;
			if(ModeKey>=5)ModeKey=0;
			FlagAC=1;
			FlagSleep=0;
			//uint32_t data=ModeKey;
			//FlashWrite(data);
		}
	}
	else if(GPIO_ReadInputBit(AC_PORT, AC_PIN)==BIT_RESET && Flagkey==0)
	{
		Cnt1=0;
		if(++Cnt2>300) //>21ms 
		{
			Cnt2=0;Flagkey=1;
			EPWM->OUT_CR = PWM_FLY; //关闭电机
			CCT_SetCompare1(CCT0,0);
			CCT_SetCompare2(CCT0,0);
			//--------------------------------20250429
//      StateFlag.bStop_done = false;
//      Stop.bBrakeFlag = false;
//			(void)Epwm_init();
//      (void)MCI_Init();
//      Stop.hBrakeCnt = Stop.hBrakeTime;
//			StateMachine = IDLE;
			PWMDuty.LedON    = 0;
			PWMDuty.Ydata    = 0;
			PWMDuty.Wdata    = 0;
			PWMDuty.YdataK    = 0;
			PWMDuty.WdataK    = 0;
			
			PWMDuty.FanON    = 0;
			OnOffTime.status = 0;
			FAN_Shake = 0;
			Command = CMD_STOP;
			FlagSleep=1;
		}
	}
	else {Cnt1=0;Cnt2=0;}
	
	if(SWOFirstflag ==0)
	{
		if(GPIO_ReadInputBit(AC_PORT, AC_PIN)==BIT_SET)
		{
			Cnt3++;
			if(Cnt3>20)
			{
				SWOFirstflag = 1;
			}
		}
		else
		{
			Cnt3 = 0;
		}
	}
}

//-------------------------------------------------------------
void SWO_reuseGPIO(void)
{
	if(SWOFirstflag&&SWOUSEflag==0)
	{
		SWOUSEflag = 1;

		LED_PWM_Init();
	}
}
//*****************************************************
void SleepMcu(void)
{
	if(FlagSleep)
	{
		//if(StateMachine == IDLE) //20250429
		{
			McuSleepCnt++;
			//if(McuSleepCnt>100) //20250429
			{
				McuSleepCnt =0;
				FlagSleep=0;
				WDG_Disable();
				
				NVIC_DisableIRQ (TIM0_IRQn);
				NVIC_DisableIRQ (EPWM_IRQn);
				
				CCT_CH1Cmd(CCT0, DISABLE);
				CCT_CH2Cmd(CCT0, DISABLE);
				EPWM->OUT_CR = 0x333; //关闭电机
				GPIO_ResetBit(GPIOD, GPIO_PIN_1);
				GPIO_ResetBit(GPIOB, GPIO_PIN_4);
				CCT_SetCompare1(CCT0,0);
				CCT_SetCompare2(CCT0,0);
				//============================PD13唤醒
				/* Config rising interrupt */
				GPIO_IntConfig(GPIOD, GPIO_PIN_13, GPIO_INT_RISING);
				/* Enable interrupt */
				NVIC_SetPriority(GPIOD_IRQn, 2);
				NVIC_EnableIRQ(GPIOD_IRQn);
				GPIO_IntEnable(GPIOD, GPIO_PIN_13);
				__NOP();__NOP();
				//===========================LPTIM唤醒
				/* Config LPTIM */
				LPTIM_InitTypeStruct LPTIM_InitType;
				LPTIM_InitType.LPTIM_PeriodValue = 32768 * 4; //6s唤醒
				LPTIM_Init(&LPTIM_InitType);
				/* Enable interrtupt */
				NVIC_SetPriority(LPTIM_IRQn, 2);
				NVIC_EnableIRQ(LPTIM_IRQn);
				LPTIM_IntCmd(ENABLE);
				LPTIM_Cmd(ENABLE);
				/* Enable clock */
				RCC_Unlock();
				/* Enable the LSCLK in the stop mode. */
				RCC_StopModeFunctionCmd(RCC_STOP_FUNC_LSCLK, ENABLE);
				/* Enable clock */
				RCC_Lock();
				
				/* Enter stop mode */
				RCC_EnterLowPowerMode(RCC_LOW_POWER_MODE_STOP, RCC_LOW_POWER_ENTRY_WFE);
				__NOP();__NOP();
				//======================唤醒后
				/* Enable interrupt */
				NVIC_DisableIRQ(GPIOD_IRQn);
				GPIO_IntDisable(GPIOD, GPIO_PIN_13);
								
				NVIC_DisableIRQ(LPTIM_IRQn);
				LPTIM_IntCmd(DISABLE);
				LPTIM_Cmd(DISABLE);
				
				//------------------------------------------20250429
				StateFlag.bStop_done = false;
				Stop.bBrakeFlag = false;
				(void)Epwm_init();
				(void)MCI_Init();
				Stop.hBrakeCnt = Stop.hBrakeTime;
				StateMachine = IDLE;
				
				CCT_CH1Cmd(CCT0, ENABLE);
				CCT_CH2Cmd(CCT0, ENABLE);
				NVIC_EnableIRQ (TIM0_IRQn);
				NVIC_EnableIRQ (EPWM_IRQn);
				Command = CMD_STOP;
				FAN_Cmd = Command;
				//FlagACOn=1;
				Watch_dog_init();
				WDG_FeedDog();
			}
		}	
	}
	else
	{
		McuSleepCnt =0;
	}
}

//************************************************
void GPIOD_IRQHandler(void)
{
  if(RESET != GPIO_GetIntFlagStatus(GPIOD,GPIO_PIN_13))
  {
    GPIO_ClearIntFlag(GPIOD, GPIO_PIN_13);
		Timer500ms.bFlag = false;
		Timer500ms.hCounter = 400; //200ms
	  FlagACOn=1;
	  
  }
}

//================================================LPTIM唤醒
void LPTIM_IRQHandler(void)
{
  if(LPTIM_GetIntFlagStatus() == SET)
  {
    LPTIM_ClearIntFlag();
		FlagACOn=2;
  }
}

